package com.fz.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import com.fz.filter.JwtAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/tRole/**").permitAll()
                .antMatchers("/tUser/Buslogin").permitAll()
                .antMatchers("/tUser/**").permitAll()
                .antMatchers("/tPermission/**").permitAll()
                .antMatchers("/userRole/**").permitAll()
                .antMatchers("/category/**").permitAll()
                .antMatchers("/cart/**").permitAll()
                .antMatchers("/goods/**").permitAll()
                .antMatchers("/marketing/**").permitAll()
                .antMatchers("/orders/**").permitAll()
                .antMatchers("/test").permitAll()
                .antMatchers("/repair/**").permitAll()
                .antMatchers("/notice/**","/inf/**").permitAll()
                .antMatchers("/tPageview/**").permitAll()
                .antMatchers("/visitor/**").permitAll()
                .antMatchers("/complaint/**").permitAll()
                .antMatchers("/propertyFee/**").permitAll()
                .antMatchers("/parkingSpace/**").permitAll()
                .antMatchers("/tUser/login","/tUser/register","/tUser/check").permitAll()
                .antMatchers("/banner/list","/complaint/list","/complaint/rej").permitAll()
                .antMatchers("/tUser/resetPassword","/tUser/verifyCode","/tUser/sendVerificationCode").permitAll()
                .antMatchers("/roleRes/**").permitAll()
                .antMatchers("/region/**").permitAll()
                .antMatchers("/stores/**").permitAll()
                .antMatchers("/regionStores/**").permitAll()
                .antMatchers("/cateStores/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .cors()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }
    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() {
        return new JwtAuthenticationFilter();
    }
    @Bean
    BCryptPasswordEncoder getPasswordEncoder () {
        return new BCryptPasswordEncoder();
    }

    //跨域器
    @Bean
    CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("http://127.0.0.1:5173");
        configuration.addAllowedOrigin("http://localhost:5173");

        configuration.addAllowedOrigin("http://localhost:8081");

        configuration.addAllowedOrigin("http://localhost");
        configuration.addAllowedOrigin("http://localhost:90");
        configuration.addAllowedMethod("*");
        configuration.addAllowedHeader("*");
        source.registerCorsConfiguration("/**", configuration);
        return new CorsFilter(source);
    }
    //分页器
    @Bean
    MybatisPlusInterceptor getInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    //redis配置
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return new StringRedisTemplate(redisConnectionFactory);
    }

    @Bean
    Converter<String, LocalDateTime> localDateTimeConverter()
    {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String s) {
                return LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            }
        };
    }

}